/*
 * ADXL335.c
 *
 * Author: Fenris Liang
 * Copyright (c) 2011
 * All rights reserved.
 *
 */

#include "ADXL335.h"
#include "stm32f10x.h"
#include "tool.h"


void ADXL335_Init(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;
	ADC_InitTypeDef		ADC_InitStructure;

	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
	
	GPIO_InitStructure.GPIO_Pin				= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_4 |GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode			= GPIO_Mode_AIN;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    ADC_InitStructure.ADC_Mode					= ADC_Mode_Independent;
    ADC_InitStructure.ADC_ScanConvMode			= DISABLE;
    ADC_InitStructure.ADC_ContinuousConvMode	= DISABLE;
    ADC_InitStructure.ADC_ExternalTrigConv		= ADC_ExternalTrigConv_None;
    ADC_InitStructure.ADC_DataAlign				= ADC_DataAlign_Right;
    ADC_InitStructure.ADC_NbrOfChannel			= 1;
    ADC_Init(ADC1, &ADC_InitStructure);
    
    ADC_Cmd(ADC1, ENABLE);
    ADC_ResetCalibration(ADC1);
    while(ADC_GetCalibrationStatus(ADC1));
    ADC_SoftwareStartConvCmd(ADC1, ENABLE);
    while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
}


unsigned short Get_ADXL335_Normal_X(void)
{
     //return GetADC1Channel(ADC_Channel_13);
	return GetADC1Channel(ADC_Channel_10);
}

unsigned short Get_ADXL335_Normal_Y(void)
{
     //return GetADC1Channel(ADC_Channel_12);
	 return GetADC1Channel(ADC_Channel_11);
}

unsigned short Get_ADXL335_Normal_Z(void)
{
     //return GetADC1Channel(ADC_Channel_10);
	 return GetADC1Channel(ADC_Channel_12);
}

unsigned short Get_ADXL335_45_X(void)
{
     return GetADC1Channel(ADC_Channel_14);
}

unsigned short Get_ADXL335_45_Y(void)
{
     return GetADC1Channel(ADC_Channel_15);
}
